/*
 * Copyright 2007 Xplayat, S.L. All rights reserved.
 * Propiedad de Xplayat. 
 */
package es.xpt.ysf.jaas;

import java.security.Principal;
import java.security.acl.Group;
import java.util.Locale;
import java.util.StringTokenizer;

@SuppressWarnings("serial")
public class YSFPrincipal implements Principal, java.io.Serializable {
	
	public enum Type {SYSTEM}
	
	private String name;
	private String userCode;
	private String application;
	private String company;
	private String entity;
	private String environment;
	private String description;
	private String email;
	private Locale locale;
	private String ipAddress;
	private Group[] roles;
	private boolean system = false;
	
	private String datasource;
	private String bigData;

	
	/**
	 * Create a <code>YSFPrincipal</code> using a <code>String</code>
	 * representation of the user name.
	 * 
	 * @param name
	 *            the user identification number (UID) for this user.
	 * 
	 */
	public YSFPrincipal(String name) {
		this.name = name;
	}
	
	public static Locale getLocale(String compositeLocaleString) {
		if (compositeLocaleString != null) {
			StringTokenizer tk = new StringTokenizer(compositeLocaleString, "_");
			String language = tk.nextToken();
			String country = null; 
			if (tk.hasMoreTokens()) {
				country = tk.nextToken();
			}
			if (country != null && country.length() > 0) {
				return new Locale(language, country);
			} else {
				return new Locale(language);
			}
		}
		return new Locale("es", "ES");
	}



	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final YSFPrincipal other = (YSFPrincipal) obj;
		if (company == null) {
			if (other.company != null)
				return false;
		} else if (!company.equals(other.company))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((company == null) ? 0 : company.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/**
	 * Return a string representation of this <code>YSFPrincipal</code>.
	 * 
	 * 
	 * @return a string representation of this <code>YSFPrincipal</code>.
	 */
	public String toString() {
		return name;
	}

	/**
	 * Return the user name for this <code>YSFPrincipal</code>.
	 * 
	 * 
	 * @return the user name for this <code>YSFPrincipal</code>
	 */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	/**
	 * @return the locale
	 */
	public Locale getLocale() {
		return locale;
	}

	/**
	 * @param locale
	 *            the locale to set
	 */
	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	/**
	 * @return the company
	 */
	public String getCompany() {
		return company;
	}

	/**
	 * @param company
	 *            the company to set
	 */
	public void setCompany(String company) {
		this.company = company;
	}

	/**
	 * @return the environment
	 */
	public String getEnvironment() {
		return environment;
	}

	/**
	 * @param environment the environment to set
	 */
	public void setEnvironment(String environment) {
		this.environment = environment;
	}

	/**
	 * @return the application
	 */
	public String getApplication() {
		return application;
	}

	/**
	 * @param application the application to set
	 */
	public void setApplication(String application) {
		this.application = application;
	}
	
	/**
	 * @return the entity
	 */
	public String getEntity() {
		return entity;
	}

	/**
	 * @param entity the entity to set
	 */
	public void setEntity(String entity) {
		this.entity = entity;
	}

	/**
	 * @return the ipAddress
	 */
	public String getIpAddress() {
		return ipAddress;
	}

	/**
	 * @param ipAddress the ipAddress to set
	 */
	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}
	
	public Group[] getRoles() {
		return roles;
	}

	public void setRoles(Group[] roles) {
		this.roles = roles;
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public boolean isSystem() {
		return system;
	}

	public void setSystem(boolean system) {
		this.system = system;
	}


	public void setDatasource(String datasource) {
		this.datasource = datasource;
	}


	public String getDatasource() {
		return datasource;
	}

	public String getBigData() {
		return bigData;
	}

	public void setBigData(String bigData) {
		this.bigData = bigData;
	}

}
